package Item
{
	import Map.CMap;
	import Map.CMapTile;
	
	import flash.display.Sprite;
	
	import srdk.SRRandom;
	
	public class CItemManager
	{
		private var m_sprite :Sprite;
		private var m_refMap :CMap;
		
		private var m_arrElapsedTimeToItemCreate :Array;
		private var m_arrTimeToCreateItem :Array;
		private var m_arrItem :Array;
		
		public function get sprite() :Sprite { return m_sprite; }
		public function get count() :uint { return m_arrItem.length; }
		
		public function CItemManager(refMap :CMap)
		{
			m_sprite = new Sprite();
			m_refMap = refMap;
			
			m_arrElapsedTimeToItemCreate = new Array();
			for (var i :uint = 0; i < CItem.TYPE_COUNT; ++i)
			{
				m_arrElapsedTimeToItemCreate.push(new Number(0));
			}
			
			m_arrTimeToCreateItem = new Array();
			for (i  = 0; i < CItem.TYPE_COUNT; ++i)
			{
				m_arrTimeToCreateItem.push(new Number(0.0));
			}
			m_arrTimeToCreateItem[CItem.TYPE_FOOD]		= 5.0;
			m_arrTimeToCreateItem[CItem.TYPE_VIRUS]		= 2.0;
			m_arrTimeToCreateItem[CItem.TYPE_COIN]		= 1.0;
			m_arrTimeToCreateItem[CItem.TYPE_GOLD]		= 7.0;
			m_arrTimeToCreateItem[CItem.TYPE_SHOWTIME]	= 20.0;
			
			m_arrItem = new Array();
		}
		public function GetItem(i :uint) :CItem
		{
			if (i >= m_arrItem.length) return null;
			return m_arrItem[i];
		}
		public function DeleteItem(i :uint) :void
		{
			var refItem :CItem = m_arrItem[i];
			
			var refMapTile :CMapTile = m_refMap.GetMapTile(refItem.nMapPosX, refItem.nMapPosY);
			if (refMapTile != null)
			{
				refMapTile.SetAttribute(CMapTile.ATT_ITEM, false);
			}
			
			sprite.removeChild(refItem.sprite);
			m_arrItem.splice(i, 1);
		}
		public function Update(tick :Number) :void
		{
			UpdateGarbageItemCollection(tick);
			
			UpdateItemFactory(tick, CItem.TYPE_FOOD);
			UpdateItemFactory(tick, CItem.TYPE_VIRUS);
			UpdateItemFactory(tick, CItem.TYPE_COIN);
			UpdateItemFactory(tick, CItem.TYPE_GOLD);
			UpdateItemFactory(tick, CItem.TYPE_SHOWTIME);
		}
		public function Clear() :void
		{
			for (var i :uint = 0; i < m_arrItem.length; ++i)
			{
				var refItem :CItem = m_arrItem[i];
				
				sprite.removeChild(refItem.sprite);
				
				var refMapTile :CMapTile = m_refMap.GetMapTile(refItem.nMapPosX, refItem.nMapPosY);
				if (refMapTile != null)
				{
					refMapTile.SetAttribute(CMapTile.ATT_ITEM, false);
				}
				
				m_arrItem[i] = null;
			}
			m_arrItem.splice(0, m_arrItem.length);
		}
		public function SetTimeToCreateItem_Food(time :Number) :void
		{
			m_arrTimeToCreateItem[CItem.TYPE_FOOD]	= time;
		}
		private function UpdateGarbageItemCollection(tick :Number) :void
		{
			for (var i :uint = 0; i < m_arrItem.length;)
			{
				var refItem :CItem = m_arrItem[i]; 
				refItem.Update(tick);
				if (refItem.bIsDestroy == true)
				{
					sprite.removeChild(refItem.sprite);
					
					var refMapTile1 :CMapTile = m_refMap.GetMapTile(refItem.nMapPosX, refItem.nMapPosY);
					if (refMapTile1 != null)
					{
						refMapTile1.SetAttribute(CMapTile.ATT_ITEM, false);
					}
					
					m_arrItem[i] = null;
					m_arrItem.splice(i, 1);
				}
				else
					++i
			}
		}
		private function UpdateItemFactory(tick :Number, nType :uint) :void
		{
			if (CheckCreateItem(tick, nType) == true)
			{
				for (var i :uint = 0; i < 10; ++i)
				{
					var nRandMapPosX :int = SRRandom.GetRand(0, m_refMap.nMapSizeX-1);
					var nRandMapPosY :int = SRRandom.GetRand(0, m_refMap.nMapSizeY-1);
					
					var refMapTile :CMapTile = m_refMap.GetMapTile(nRandMapPosX, nRandMapPosY);
					if (refMapTile != null)
					{
						if (refMapTile.nType != CMapTile.TYPE_NORMAL) continue;
						if (refMapTile.GetAttribute(CMapTile.ATT_ITEM) == true) continue;
						
						refMapTile.SetAttribute(CMapTile.ATT_ITEM, true);
						
						var cItem :CItem = CreateItem(nType);
						cItem.SetMapPosition(nRandMapPosX, nRandMapPosY);
						
						m_arrItem.push(cItem);
						sprite.addChild(cItem.sprite);
						break;
					}
				}
			}
		}
		private function CheckCreateItem(tick :Number, nType :uint) :Boolean
		{
			m_arrElapsedTimeToItemCreate[nType] += tick;
			if (m_arrElapsedTimeToItemCreate[nType] > m_arrTimeToCreateItem[nType])
			{
				m_arrElapsedTimeToItemCreate[nType] = 0;
				return true;
			}
			return false;
		}
		private function CreateItem(nType :uint) :CItem
		{
			switch(nType)
			{
			case CItem.TYPE_FOOD:		return new CItem_Food();
			case CItem.TYPE_VIRUS:		return new CItem_Virus();
			case CItem.TYPE_COIN:		return new CItem_Coin();
			case CItem.TYPE_GOLD:		return new CItem_Gold();
			case CItem.TYPE_SHOWTIME:	return new CItem_ShowTime();
			}
			return null;
		}
		
	}
}